package com.example.zhunongzonghe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.example.zhunongzonghe.common.ResultFile;
import com.example.zhunongzonghe.exception.FileFormatException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description 文件上传
 */
@Api(tags = "文件上传接口")
@RestController
@RequestMapping("/file")
public class FilesUploadController {
    @Value("${application.upload-path}")
    private String fileDirectory;

    //    头像上传
    @ApiOperation(value = "头像上传")
    @PostMapping("/upload/{type}")
    public ResultFile<String> upload(@PathVariable("type") String type,
                                     @RequestParam("file") MultipartFile file) throws Exception {

        System.out.println("!!!!请求图片=" + file);
        String originalFilename = file.getOriginalFilename();
        String tail = originalFilename.substring(originalFilename.lastIndexOf("."));
//    图片常见的格式有：bmp、jpg、tiff、gif、pcx、tga、exif、fpx、svg、psd、cdr、pcd、dxf、ufo、eps、ai、raw等
        if (!tail.equals(".jpg") & !tail.equals(".gif") & !tail.equals(".png") & !tail.equals(" eg") &
                !tail.equals(".webp") & !tail.equals(".mp4")) {
            throw new FileFormatException("请传入正确格式文件");
        }
//        创建目录
        String header = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = header + tail;
        String targetFileLocation = fileDirectory + File.separator + type;
        File file1 = new File(targetFileLocation);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        //创建文件
        String targetFileName = targetFileLocation + File.separator + newFileName;
        File targetFile = new File(targetFileName);
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        file.transferTo(targetFile);
        return new ResultFile<>(true, 200, "上传成功", newFileName);
    }

    //    资料上传
    @ApiOperation(value = "资料上传")
    @PostMapping("/uploadInfo/{type}")
    public ResultFile<String> uploadInfo(@PathVariable("type") String type,
                                         @RequestParam("file") MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String tail = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!tail.equals(".jpg") & !tail.equals(".gif") & !tail.equals(".png") & !tail.equals(" eg") &
                !tail.equals(".webp") & !tail.equals(".ogm") & !tail.equals(".wmv") & !tail.equals(".mpg") & !tail.equals(".webm") &
                !tail.equals(".ogv") & !tail.equals(".mp4") & !tail.equals(".mov")& !tail.equals(".mov")
                & !tail.equals(".asx") & !tail.equals(".mpeg")& !tail.equals(".m4v")& !tail.equals(".avi")) {
            throw new FileFormatException("请传入正确格式文件");
        }
//        创建目录
        String header = UUID.randomUUID().toString().replaceAll("-", "");
//        Calendar calendar = Calendar.getInstance();
//        Date time = calendar.getTime();
//        String header = time.toString();
        String newFileName = header + tail;
        String targetFileLocation = fileDirectory + File.separator + type;
        File file1 = new File(targetFileLocation);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        //创建文件
        String targetFileName = targetFileLocation + File.separator + newFileName;
        File targetFile = new File(targetFileName);
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        file.transferTo(targetFile);
        return new ResultFile<>(true, 200, "上传成功", newFileName);
    }

    @ApiOperation(value = "多文件上传")
    @PostMapping("/uploadList/{type}")
    public ResultFile<List<String>> uploadInfo(@PathVariable("type") String type,
                                               @RequestParam("file") List<MultipartFile> files) throws Exception {
        List<String> fileNames = new ArrayList<>();

        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            String tail = originalFilename.substring(originalFilename.lastIndexOf("."));

            // 校验文件格式
            if (!tail.equals(".jpg") & !tail.equals(".gif") & !tail.equals(".png") & !tail.equals(" eg") &
                    !tail.equals(".webp") & !tail.equals(".ogm") & !tail.equals(".wmv") & !tail.equals(".mpg") & !tail.equals(".webm") &
                    !tail.equals(".ogv") & !tail.equals(".mp4") & !tail.equals(".mov")& !tail.equals(".mov")
                    & !tail.equals(".asx") & !tail.equals(".mpeg")& !tail.equals(".m4v")& !tail.equals(".avi")) {
                throw new FileFormatException("请传入正确格式文件");
            }

            // 创建目录
            String header = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = header + tail;
            String targetFileLocation = fileDirectory + File.separator + type;
            File file1 = new File(targetFileLocation);
            if (!file1.exists()) {
                file1.mkdirs();
            }

            // 创建文件
            String targetFileName = targetFileLocation + File.separator + newFileName;
            File targetFile = new File(targetFileName);
            if (!targetFile.exists()) {
                targetFile.createNewFile();
            }

            // 文件上传
            file.transferTo(targetFile);
            fileNames.add(newFileName);
        }

        return new ResultFile<>(true, 200, "上传成功", fileNames);
    }


    @ApiOperation(value = "富文本编辑器图片上传")
    @PostMapping("/editUpload/{type}")
    public Dict editorUpload(@PathVariable("type") String type,
                                     @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("!!!!请求图片=" + file);
        String originalFilename = file.getOriginalFilename();
        String tail = originalFilename.substring(originalFilename.lastIndexOf("."));
        //    图片常见的格式有：bmp、jpg、tiff、gif、pcx、tga、exif、fpx、svg、psd、cdr、pcd、dxf、ufo、eps、ai、raw等
        if (!tail.equals(".jpg") & !tail.equals(".gif") & !tail.equals(".png") & !tail.equals(" eg") &
                !tail.equals(".webp") & !tail.equals(".mp4")) {
            throw new FileFormatException("请传入正确格式文件");
        }
        //        创建目录
        String header = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = header + tail;
        String targetFileLocation = fileDirectory + File.separator + type;
        File file1 = new File(targetFileLocation);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        //创建文件
        String targetFileName = targetFileLocation + File.separator + newFileName;
        File targetFile = new File(targetFileName);
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        file.transferTo(targetFile);
        String url = "http://localhost:9000/" + "api/file/"+newFileName;
        Dict dict = Dict.create().set("errno",0).set("data", CollUtil.newArrayList(Dict.create().set("url",url)));
        return dict;
    }
    @ApiOperation(value = "富文本编辑器视频上传")
    @PostMapping("/editVideoUpload/{type}")
    public Dict editorVideoUpload(@PathVariable("type") String type,
                             @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("!!!!请求图片=" + file);
        String originalFilename = file.getOriginalFilename();
        String tail = originalFilename.substring(originalFilename.lastIndexOf("."));
        //    图片常见的格式有：bmp、jpg、tiff、gif、pcx、tga、exif、fpx、svg、psd、cdr、pcd、dxf、ufo、eps、ai、raw等
        if (!tail.equals(".ogm") & !tail.equals(".wmv") & !tail.equals(".mpg") & !tail.equals(".webm") &
                !tail.equals(".ogv") & !tail.equals(".mp4") & !tail.equals(".mov")& !tail.equals(".mov")
                & !tail.equals(".asx") & !tail.equals(".mpeg")& !tail.equals(".m4v")& !tail.equals(".avi")
        ) {
            throw new FileFormatException("请传入正确格式文件");
        }
        //        创建目录
        String header = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = header + tail;
        String targetFileLocation = fileDirectory + File.separator + type;
        File file1 = new File(targetFileLocation);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        //创建文件
        String targetFileName = targetFileLocation + File.separator + newFileName;
        File targetFile = new File(targetFileName);
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        file.transferTo(targetFile);
        String url = "http://localhost:9000/" + "api/file/"+newFileName;
        Dict dict = Dict.create().set("errno",0).set("data", Dict.create().set("url",url));
        return dict;
    }
}
